package asyncsenders;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.ServiceAdapter;
import flex.messaging.util.UUIDUtils;

public class RPCSender extends ServiceAdapter {

	@Override
	public Object invoke(Message message) {
		AsyncMessage newMessage = (AsyncMessage) message;
		MessageService msgService = (MessageService) getDestination()
				.getService();
		msgService.pushMessageToClients(newMessage, true);
		return null;
	}

	public void pushOpponentMove(String toUser, String winner,
			String opponentTurn) {
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("rpcPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", toUser);
		msg.setHeader("type", "turn end");
		msg.setHeader("winner", winner);
		msg.setHeader("opponentTurn", opponentTurn);
		msg.setBody("");
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);

	}

	public void pushGameFinished(String toUser, String winner, String oppTurn) {
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("rpcPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", toUser);
		msg.setHeader("type", "game end");
		msg.setHeader("winner", winner);
		msg.setHeader("opponentTurn", oppTurn);
		msg.setBody("");
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);
	}

}
